//
// Created by xili on 2024/10/5 20:03.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int maxScore(vector<int> &cardPoints, int k) {
        int n = cardPoints.size();
        int totalSum = accumulate(cardPoints.begin(), cardPoints.end(), 0); // 计算所有卡片的总和
        int windowSum = accumulate(cardPoints.begin(), cardPoints.begin() + (n - k), 0); // 初始窗口为前 n-k 个元素
        int minWindowSum = windowSum;
        for (int i = n - k; i < n; i++) {
            windowSum += cardPoints[i] - cardPoints[i - (n - k)]; // 移动窗口，添加一个新元素，去掉窗口最左边的元素
            minWindowSum = min(minWindowSum, windowSum); // 找到最小窗口和
        }
        return totalSum - minWindowSum; // 总和减去最小的窗口和即为答案
    }
};
